package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Admin;
import com.xxx.entity.dto.AdminDto;
import com.xxx.service.IAdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/admin")
@Tag(name = "管理员管理")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @PostMapping("/add")
    @Operation(summary = "添加管理员")
    public Result add(@RequestBody Admin admin){
        Boolean flag = adminService.save(admin);
        return flag ? Result.success("添加管理员成功") : Result.error("添加管理员失败");
    }

    @GetMapping("/delete/{adminId}")
    @Operation(summary = "删除管理员")
    public Result delete(@PathVariable Integer adminId){
        Boolean flag = adminService.removeById(adminId);
        return flag ? Result.success("删除管理员成功") : Result.error("不存在该管理员Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改管理员")
    public Result update(@RequestBody Admin admin){
        Boolean flag = adminService.updateById(admin);
        return flag ? Result.success("修改管理员成功") : Result.error("修改管理员失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询管理员")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Admin> page = adminService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据管理员名、管理员工号、管理员Id条件查询管理员")
    public Result selectByCondition(@RequestBody AdminDto adminDto){
        LambdaQueryWrapper<Admin> lqw = new LambdaQueryWrapper<>();
        lqw.like(adminDto.getAdminName()!=null&&!adminDto.getAdminName().equals(""),
                        Admin::getAdminName,adminDto.getAdminName())
                .eq(adminDto.getAdminWorkId()!=null&&!adminDto.getAdminWorkId().equals("")
                        ,Admin::getAdminWorkId,adminDto.getAdminWorkId())
                .eq(adminDto.getAdminId()!=null,Admin::getAdminId,adminDto.getAdminId());
        IPage<Admin> page = adminService.page(new Page<>(adminDto.getCurrent(), adminDto.getSize()),
                lqw);
        return Result.success(page);
    }
}

